<template>
  <div class="hzy_edit_employee">
    <!-- 编辑员工表格 -->
    <el-dialog
      title="编辑员工"
      :visible.sync="dialogVisible"
      width="1000px"
      :before-close="handleClose"
    >
      <span>
        <el-form :inline="true" class="demo-form-inline" :model="editEmployee">
          <el-form-item label="员工编码">
            <el-input :disabled="true" :value="editEmployee.nameId"></el-input>
          </el-form-item>
          <el-form-item label="用户名">
            <el-input v-model="editEmployee.userName"></el-input>
          </el-form-item>
          <el-form-item label="角色名称">
            <el-select placeholder="请选择" v-model="editEmployee.jiaoSe">
              <el-option
                v-for="item in jiaoseGroup"
                :key="item.jiaoseid"
                :value="item.jiaoseid"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="员工姓名">
            <el-input v-model="editEmployee.name"></el-input>
          </el-form-item>
          <el-form-item label="身份证号">
            <el-input v-model="editEmployee.idCard"></el-input>
          </el-form-item>
          <el-form-item label="联系电话">
            <el-input v-model="editEmployee.phone"></el-input>
          </el-form-item>
          <el-form-item label="所属门店">
            <el-select placeholder="请选择" v-model="editEmployee.fenDianName">
              <el-option
                v-for="item in fendianGroup"
                :key="item.fendianid"
                :value="item.fendianid"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="所属分组">
            <el-select placeholder="请选择" v-model="editEmployee.zuMing">
              <el-option
                v-for="item in fenzuGroup"
                :key="item.fenzuid"
                :value="item.fenzuid"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="在职状态：">
            <el-radio :value="editEmployee.zhuangTai" :label="true"
              >在职</el-radio
            >
            <el-radio :value="editEmployee.zhuangTai" :label="false"
              >离职</el-radio
            >
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button :plain="true" type="primary" @click="SaveMsg"
          >保存</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { editEmployeeApi } from "@/http/employeeListApi";

export default {
  name: "EditEmployee",
  props: ["isShowEdit", "editList"],
  mounted() {
    this.dialogVisible = this.isShowEdit;
  },
  watch: {
    dialogVisible: function () {
      if (!this.dialogVisible) {
        this.$emit("closeEdit");
      }
    },
  },
  data() {
    return {
      dialogVisible: true,
      radio: "1",
      editEmployee: this.editList,
      editEmpty: {},
      jiaoseGroup: [
        // 角色名称
        {
          jiaoseid: "销售组",
        },
        {
          jiaoseid: "采购组",
        },
      ],
      fendianGroup: [
        // 所属门店
        {
          fendianid: "望京分店",
        },
        {
          fendianid: "王府井分店",
        },
      ],
      fenzuGroup: [
        {
          fenzuid: "A组",
        },
        {
          fenzuid: "B组",
        },
      ],
    };
  },
  methods: {
    handleClose(done) {
      // 关闭编辑界面
      this.$confirm("确认关闭？")
        .then(() => {
          done();
        })
        .catch(() => {});
    },
    EditInfo() {
      // 触发父组件编辑按钮
      this.$emit("editClick");
      this.dialogVisible = true;
    },
    SaveMsg() {
      // 保存编辑
      this.editEmpty = this.editEmployee;
      console.log(this.editEmpty);
      this.dialogVisible = false;
      
      editEmployeeApi(this.editEmpty)
        .then((res) => {
          this.$emit("getInit"); // 重新渲染
          console.log("编辑成功：", res);

          this.$message({
            message: "保存成功",
            type: "success",
          });
        })
        .catch((err) => {
          console.log("编辑失败：", err);
        });
    },
  },
};
</script>